
package com.rt.schedulenew.web;

import com.rt.schedulebase.entity.ExamSmsNotice;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import com.rt.schedulenew.utils.pkgenerator.PKGeneratorCluster;
import org.springframework.web.bind.annotation.RequestBody;
import com.rt.schedulenew.dto.AlarmDto;

import java.util.List;
import javax.annotation.Resource;

import com.rt.schedulenew.api.IExamSmsNoticeService;
import org.slf4j.Logger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({"/alarm"})
public class AlarmController {
    @Resource
    private IExamSmsNoticeService examSmsNoticeService;
    private static Logger log = LoggerFactory.getLogger(AlarmController.class);

    @PostMapping({"/receive"})
    public void receive(@RequestBody List<AlarmDto> alarmList) {
        String content = this.getContent(alarmList);
        log.error("skywalking告警消息已发送...{}", content);
        ExamSmsNotice examSmsNotice = new ExamSmsNotice();
        examSmsNotice.setSmsNo(PKGeneratorCluster.nextPkStr());
        examSmsNotice.setSmsContent(content);
        this.examSmsNoticeService.insert(examSmsNotice);
    }

    private String getContent(List<AlarmDto> alarmList) {
        StringBuilder sb = new StringBuilder();
        for (AlarmDto dto : alarmList) {
            sb.append("scopeId: ").append(dto.getScopeId()).append("\nscope: ").append(dto.getScope()).append("\n目标 Scope 的实体名称: ").append(dto.getName()).append("\nScope 实体的 ID: ").append(dto.getId0()).append("\nid1: ").append(dto.getId1()).append("\n告警规则名称: ").append(dto.getRuleName()).append("\n告警消息内容: ").append(dto.getAlarmMessage()).append("\n告警时间: ").append(dto.getStartTime()).append("\n\n---------------\n\n");
        }
        return sb.toString();
    }
}
